﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace MMS.DAL
{
    /// <summary>
    /// 数据库操作
    /// </summary>
    public class SqlServerDBHelper : DBHelper
    {
        private static string DB_CONNECTION_STRING = @"Data Source=.\SQLEXPRESS;Initial Catalog=mmsdb;Integrated Security=True";

        private SqlConnection conn = new SqlConnection(DB_CONNECTION_STRING);

        public override DataTable GetTable(string sql)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);//创建DataAdapter读取数据并填充
            da.Fill(dt);
            return dt;
        }


        public override int ExecuteSQL(string sql)
        {
            Open();
            int count = new SqlCommand(sql, conn).ExecuteNonQuery();
            Close();
            return count;
        }

        public override object ExecuteScalar(string sql)
        {
            Open();
            object data = new SqlCommand(sql, conn).ExecuteScalar();
            Close();
            return data;
        }

        public override System.Data.Common.DbDataReader GetReader(string sql)
        {
            Open();
            return new SqlCommand(sql, conn).ExecuteReader();
        }

        public override void Open()
        {
            conn.Open();
        }

        public override void Close()
        {
            conn.Close();
        }
    }
}
